<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    :close-on-click-modal="false"
    width="800px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="105px">
      <el-row :gutter="10" >
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="课程名称" prop="name">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="授课教师" prop="teacher">
            <el-input v-model="form.teacher" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="课程阶段（0本科/1硕士）" prop="phase">
            <el-input v-model="form.phase" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="开课学期（如2023秋季）" prop="semester">
            <el-date-picker
                    v-model="form.semester"
                    type="date"
                    placeholder="选择开课学期（如2023秋季）"
                    style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="学院" prop="department">
            <el-input v-model="form.department" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="专业" prop="major">
            <el-input v-model="form.major" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="提交用户ID" prop="submittedBy">
            <el-input v-model="form.submittedBy" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="审批管理员ID" prop="approvedBy">
            <el-input v-model="form.approvedBy" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doInsert, doUpdate } from "@/api/website/course/CourseManagementApi";
  import { isNull } from "@/utils/validate";
  import { formateDate } from "@/utils/format";
  import { validatorRule } from "@/utils/validateRlue";

  export default {
    name: "CourseManagementEdit",
    data() {

      return {
        form: {
          // 设置默认值
          version: 0
        },
        dict: {},
        rules: {
              name: [
                { required: true, trigger: "blur", message: "课程名称非空" },
              ],
              teacher: [
                { required: true, trigger: "blur", message: "授课教师非空" },
              ],
              phase: [
                { required: true, trigger: "blur", message: "课程阶段（0本科/1硕士）非空" },
              ],
              semester: [
                { required: true, trigger: "blur", message: "开课学期（如2023秋季）非空" },
              ],
              department: [
                { required: true, trigger: "blur", message: "学院非空" },
              ],
              major: [
                { required: true, trigger: "blur", message: "专业非空" },
              ],
              submittedBy: [
                { required: true, trigger: "blur", message: "提交用户ID非空" },
                { required: false, trigger: "blur", validator: validatorRule.IS_INTEGER },
              ],
              approvedBy: [
                { required: true, trigger: "blur", message: "审批管理员ID非空" },
                { required: false, trigger: "blur", validator: validatorRule.IS_INTEGER },
              ],
        },
        title: "",
        dialogFormVisible: false,
      };
    },
    created() {

    },
    mounted() {
      // 加载字典值
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = "添加";
        } else {
          this.title = "编辑";
          this.form = Object.assign({}, row);
        }
        this.dialogFormVisible = true;
      },
      close() {
        this.dialogFormVisible = false;
        this.$refs["form"].resetFields();
        this.form = this.$options.data().form;
      },
      save() {
        this.$refs["form"].validate(async (valid) => {
          if (valid) {
            // 处理数据
            this.handlerFormData(this.form);

            // 修改
            if (!isNull(this.form.id)) {
              const { msg } = await doUpdate(this.form);
  			  this.$baseMessage(msg, "success");
            } else {
              const { msg } = await doInsert(this.form);
              this.$baseMessage(msg, "success");
            }

            await this.$emit("fetchData");
            this.close();
          } else {
            return false;
          }
        });
      },
      // 处理 form数据
      handlerFormData(formData){
        if(!isNull(formData)){
          for(let key in formData){
            // 对于时间类进行处理
            if("[object Date]" === Object.prototype.toString.call(formData[key])){
              formData[key] = formateDate(formData[key], 'yyyy-MM-dd hh:mm:ss');
            }
          }
        }
      },
    },
  };
</script>
